package Test;

import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import Task25.Controller;

public class copyTest {

	@Test
	public void copyAbsFiles() {
		String srcPath = "C:\\Test\\Test1\\1.txt";
		String dstPath = "C:\\Test\\Test2\\1.txt";
		command(srcPath, dstPath);
		assertTest(srcPath, dstPath, "file");
	}

	@Test
	public void copyRelFiles() {
		// change the work directory on the Test
		Controller.gui(new String[] { "cd", "C:\\Test" });
		String srcPath = "Test1\\1.txt";
		String dstPath = "Test2\\1.txt";
		command(srcPath, dstPath);
		assertTest("C:\\Test\\" + srcPath, "C:\\Test\\" + dstPath, "file");
	}

	@Test
	public void copyAbsEmptyFolder() {
		String srcPath = "C:\\Test\\Test2";
		String dstPath = "C:\\Test\\Test3\\Test2";
		command(srcPath, dstPath);
		assertTest(srcPath, dstPath, "dir");
	}

	@Test
	public void copyRelEmptyFolder() {
		// change the work directory on the Test
		Controller.gui(new String[] { "cd", "C:\\Test" });
		String srcPath = "Test2";
		String dstPath = "Test3\\Test2";
		command(srcPath, dstPath);
		assertTest("C:\\Test\\" + srcPath, "C:\\Test\\" + dstPath, "dir");
	}

	@Test
	public void copyAbsFolder() {
		String srcPath = "C:\\Test\\Test1";
		String dstPath = "C:\\Test\\Test3\\Test1";
		command(srcPath, dstPath);
		assertTest(srcPath, dstPath, "dir");
	}

	@Test
	public void copyRelFolder() {
		// change the work directory on the Test
		Controller.gui(new String[] { "cd", "C:\\Test" });
		String srcPath = "Test1";
		String dstPath = "Test3\\Test1";
		command(srcPath, dstPath);
		assertTest("C:\\Test\\" + srcPath, "C:\\Test\\" + dstPath, "dir");
	}

	// the created command and transfered him in the Controller
	private void command(String srcPath, String dstPath) {
		String[] command = { "copy", srcPath, dstPath };
		Controller.gui(command);
	}

	// the verified the work of command
	private void assertTest(String srcPath, String dstPath, String type) {
		boolean flag = false;
		flag = new File(dstPath).exists();
		if (type.equals("file"))
			flag = new File(dstPath).isFile();
		if (type.equals("dir"))
			flag = new File(dstPath).isDirectory();
		assertTrue(flag);
		del(dstPath);
	}

	// delete the destination file or folder
	private void del(String path) {
		String[] command = { "del", path };
		Controller.gui(command);
	}
}
